// packages 

var 
rialto = {};

ria=rialto;

rialto.widget = {};

rialto.widgetBehavior = {};

rialto.lang = {};

rialto.collection={};

rialto.session = {	
	count:0,
	objects:{},
	reccord:function(obj,id){
		this.objects[id]=obj;	
		this.count+=1;
	}
	,
	removeAll:function(){	
		for(obj in this.objects){
	 		if (this.objects[obj]){
	 			//alert(obj+" "+this.objects[obj].id);
	 			if (this.objects[obj].remove){
			 		this.objects[obj].remove();
			 	}
		 	}
		 	else{
		 		this.objects[obj]=null;
		 	}
	 	}
	},
	oParameters:{}
};

rialto.config= rialtoConfig;

if (rialto.config.isDesign) {
	rialto.composer = {mode : 'design'};
	rialto.composer.toogleMode = function (e){
		var newMode = (rialto.composer.mode == rialtoConfig.extIdCssComposerTryMode)
						? rialtoConfig.extIdCssComposerDesignMode
						: rialtoConfig.extIdCssComposerTryMode;
		linkCssDesign = document.getElementById(	rialtoConfig.baseIdCssComposer
												  + '_mode_'
												  + rialto.config.extIdCssComposerDesignMode);
		linkCssTry 	  = document.getElementById(	rialto.config.baseIdCssComposer
												  + '_mode_'
												  + rialto.config.extIdCssComposerTryMode);

		linkCssDesign.disabled  = ( newMode != rialto.config.extIdCssComposerDesignMode);
		linkCssTry.disabled 	= ( newMode != rialto.config.extIdCssComposerTryMode);
		rialto.composer.mode 	= newMode;

		traceExec('composer mode : '+rialto.composer.mode,73);
	}
	var obj=document.getElementById(	rialto.config.baseIdCssComposer
												  + '_mode_'
												  + rialto.config.extIdCssComposerTryMode)
	if (obj) obj.disabled = true;
}


//Global variable

window.__styleDeco='3D';

//------------------------------------------------------------------------
rialto.deprecated = function(object,oldMethod,newMethod) {
	alert(object+'.'+oldMethod+' deprecated; use '+object+'.'+newMethod);
}

/*****************SOME BASIC FUNCTION**************************/	
rialto.lang={
	addMethods : function(proto, methodList){
		for(var m in methodList){
			proto[m] = methodList[m];
		}
		return proto;
	}
	,
	extendObject : function(proto, methodList){
		this.addMethods(proto.prototype, methodList);
	}
	,
	extend : function(proto, methodList){
		this.extendObject(proto, methodList);
	}
	,	
	isString : function(ob,bNotEmpty) {		
		if(bNotEmpty){
			return ((ob instanceof String || typeof ob == "string") && ob!="");
		}
		else{
			return (ob instanceof String || typeof ob == "string");
		}
	}
	,
	isStringIn : function(ob,arr) {
		if(this.isString(ob)){
			return rialto.array.indexOf(arr,ob) != -1;		
		}
	}
	,
	isArray : function(ob) {
		return (ob instanceof Array||typeof ob=="array");
	}
	,
	isDate : function(ob) {
		return (ob instanceof Date);
	}
	,
	isBoolean : function(ob) {
		return (ob instanceof Boolean || typeof ob == "boolean");
	}
	,
	isNumber: function(ob) {
		return (ob!=null && !isNaN(parseInt(ob)));
	}
}

rialto.Dom = {
	isDescendantOf : function (nd1,nd2,orSelf){
	// return true if n1 is descendant of n2 in Dom	
		if (orSelf && (nd1 == nd2)) return true;
		
		var n1 = nd1, n2= nd2;
		n1 = n1.parentNode;
		while(n1) {
			if(n1 == n2){ return true; }
			n1 = n1.parentNode;
		}
		return false;
	}
	
	,isCoveredBy : function (nd1,nd2){
	 // method not completly tested : to verify
	 // return true if n2 covered (or partally covered) n1 
		
		var n1 = nd1, n2= nd2;
		//-- same parent : compare zIndex
		if (n1.parentNode == n2.parentNode) {
			var styleN1 = _rum.$getStyle(n1,'z-index');	
			var styleN2 = _rum.$getStyle(n2,'z-index');	
			//-- all zindex fixed
			if (styleN1 && styleN2) {
				return (styleN1 > styleN2);
			}
			//-- only one fixed
			else
			if (styleN1)
				return true;
			else
			if (styleN2)
				return false;
				
			//-- nor n1 nor n2 is fixed
			else {
				// n2 covered n1 if n2 is next*sibbling of n1
				n1 = n1.nextSibling;
				while(n1) {
					if(n1 == n2){ 
						return true; 
					}
					n1 = n1.nextSibling;
				}
				return false;		
			}
		}
		//-- n1 and n2 haven't the same parent
		else {
			var childN1AscList = {};
			// fabrik list of n1s'ascendant 
			var n1p = n1.parentNode;
			var pn1p = n1;
			while (n1p) {
				if (!n1p.id) n1p.id = 'domElt_' + rialto.Dom.nbInstances++;
				childN1AscList[n1p.id] = pn1p;
				pn1p = n1p;
				n1p = n1p.parentNode;
			}
			
			var	pn2p = n2p;
			var n2p  = n2.parentNode;
			while (n2p) {
				// common parent
				if (   n2p.id 
				    && childN1AscList[n2p.id]) {
				   // pn2p and childN1AscList[n2p.id] is sibbling
					// n2 covered n1 if pnp2 is next*sibbling of childN1AscList[n2p.id]
					var siblAscN1 = childN1AscList[n2p.id].nextSibling;
					
					while(siblAscN1) {
						
						if(siblAscN1 == pn2p){ 
							return true; 
						}
						siblAscN1 = siblAscN1.nextSibling;
					}
					return false;		
				}
				// next parent on n2 branche
				else {
					pn2p = n2p;
					n2p = n2p.parentNode;
				}
			}
		}
	}	
}
rialto.Dom.nbInstances = 0;

/*****************EXTEND OBJECT STRING**************************/
rialto.string={
	// Trim (remove whitespace)
	trim : function(str) {
	  return str.replace(/^\s*(\b.*\b|)\s*$/, "$1");
	}
	,
    //Replace each strFind by strEmp in str
	replace : function(str,strFind,strRemp) {
	  var tab = str.split(strFind)
	  return new String(tab.join(strRemp));
	}
	,
	//format the string to pass it in http request
	formatHTTP : function(str) {
	    if(rialto.lang.isString(str)){
	    	return escape(str);
	    }	    
	}
}
/*****************EXTEND OBJECT ARRAY**************************/
rialto.array={
	add : function (arr,obj) {
	//=================
	// add obj in arr if it is not already in
	  if (rialto.array.indexOf(arr,obj) == -1)
	     arr.push(obj);
	}
	,
	remove : function (arr,obj) {
	//====================
	//remove obj from arr if it is in 
		if (arr[arr.length-1] == obj)
		  arr.pop();
		else{
		  var pos =  rialto.array.indexOf(arr,obj);
		  if (pos != -1)
		     arr.splice(pos,1)[0];
		}
		return arr.length;
	}
	,
	indexOf : function (arr,obj) {
	//===================
	//return index of obj in arr or -1 if it is not in
	     var i = 0;
	     for(i=0;i<arr.length;i++){
	         if (arr[i] == obj)
		         return i;
	     }
	     return  -1;
	}
	,
	insert:function(arr,ind,obj){
	//===================
	//insert obj in arr at ind index (ind must be < arr.length)	
		arr.splice(ind,0,obj);
	}
}

/*****************EXTEND OBJECT DATE**************************/
rialto.date={
	_DayLong:new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'),
	
	toDay:function(){
		return new Date();
	}
	,
	getWeekNumber:function(date){
	//===================
	//return the number of the week of the date parameter	    
		var jour= date.getDate();    
		var mois= date.getMonth()+1;    
		var annee= date.getFullYear()
		
		var a = Math.floor((14-(mois))/12);
		var y = annee+4800-a;
		var m = (mois)+(12*a)-3;
		var jd = jour + Math.floor(((153*m)+2)/5) + 
		             (365*y) + Math.floor(y/4) - Math.floor(y/100) + 
		             Math.floor(y/400) - 32045;      // (gregorian calendar)
		var d4 = (jd+31741-(jd%7))%146097%36524%1461;
		var L = Math.floor(d4/1460);
		var d1 = ((d4-L)%365)+L;
		numSem = Math.floor(d1/7) + 1;
		return numSem;
	} 
	,  
	getBornes:function(numSem){
    //===================
	//return an array of the two date borning the week
	    var sem53=false;
	    //today date
	    var dateJour= this.toDay();
	    //1st of january
	    var datePremJanv= new Date(dateJour.getFullYear(),0,1);
	    //day of 1st of january
	    var jourSem1=datePremJanv.getDay()-1;
	    if (jourSem1<0)jourSem1=6;	    
	    //week must contain 4 day
	    if (jourSem1>3){
	        sem53=true;
	        //remove one week
	        numSem+=1;
	    }
	    if (numSem==1 && !sem53){
	        var date1=datePremJanv;
	        var date2=new Date(datePremJanv.getFullYear(),0,1+6-jourSem1);
	    }
	    else{
	        if(numSem==53 && sem53){
				var date1=new Date(datePremJanv.getFullYear()-1,11,31-jourSem1+1);
				var date2=new Date(datePremJanv.getFullYear(),0,1+6-jourSem1);
	        }
	        else{
		        var jour= 1+(numSem-1)*7;
		        var date=new Date(dateJour.getFullYear(),0,jour);	         
	            var date1=new Date(date.getFullYear(),date.getMonth(),date.getDate()-jourSem1);
		        var date2=new Date(date.getFullYear(),date.getMonth(),date.getDate()-jourSem1+6);
	        }
	    }
	    return [date1,date2]
    }
	,
	getDDMMYYYY:function(date){
	    //return dd/mm/yyyy from a date object
	    var jour= date.getDate();	    
	    if (jour<10)(jour="0"+jour)
	    var mois= date.getMonth()+1;
	    if (mois<10)(mois="0"+mois)
	    var annee= date.getFullYear()
	    return jour+"/"+mois+"/"+annee;
	}
	,
	getYYYYMMDD:function(date){	    
	    //return yyyymmdd from a date object	
	    var jour= date.getDate();
	    if (jour<10)(jour="0"+jour)
	    var mois= date.getMonth()+1;
	    if (mois<10)(mois="0"+mois)
	    var annee= date.getFullYear()
	    return annee+""+mois+""+jour;
	}
	,
	getLibJour:function(date){
	    return this._DayLong[date.getDay()];
	}
	,
	DDMMYYYYfromYYYYMMDD : function(wdate){
	    //return dd/mm/yyyy from string wdate (AAAAMMJJ)
	    jour=wdate.substr(6,2);
	    mois=wdate.substr(4,2);
	    annee=wdate.substr(0,4);
	    if (jour.length==1){jour="0"+jour;};
	    if (mois.length==1){mois="0"+mois;};
	    return jour+"/"+mois+"/"+annee;      
	}
	,
	YYYYMMDDfromDDMMYYYY: function(wdate){
        //return YYYYMMDD from string wdate (dd/mm/yyyy)
        jour=wdate.substr(0,2);
        mois=wdate.substr(3,2);
        if (jour.length==1){jour="0"+jour;};
        if (mois.length==1){mois="0"+mois;};
        annee=wdate.substr(6,4);
        return  annee+""+mois+""+jour;
    }
    ,
    setDateFromYYYYMMDD:function(wdate){
    //return a date object from string wdate (AAAAMMJJ)
    	jour=wdate.substr(6,2);
	    mois=wdate.substr(4,2);
	    annee=wdate.substr(0,4);
    	return new Date(annee,parseInt(mois,10)-1,jour);
    }
    ,
    equals:function(date1,date2)  {
		 //Return true if the 2 date are equals (only on Year,month and day)
		 return (this.getYYYYMMDD(date1)==  this.getYYYYMMDD(date2)); 
	}
	,
	//return true ig the giving string (DD/MM/YYYY) exist
	isDate:function(sDate){
	    
	    var e = new RegExp("^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$");
	
		//first test the regular expression
	    if (!e.test(sDate)) 
	        return false; 
	
	    //take the day, the month and the year
	    var arrDate=sDate.split("/");
	    var d = parseInt(arrDate[0],10); 
	    var m = parseInt(arrDate[1],10); 
	    var y = parseInt(arrDate[2]); 
	    var feb=28;	   
	    // Last day of february
	    if(this.isBissextile(y)) feb = 29;
	
	    // Number of day in each month
	    var nbJours = new Array(31,feb,31,30,31,30,31,31,30,31,30,31);
	
	    //true if the day is ok for the month
	    return ( m >= 1 && m <=12 && d >= 1 && d <= nbJours[m-1] );
	}
	,
	isBissextile:function(an) {
	  return  ((( an % 4 == 0 ) 
	  		&& ( an % 100 != 0 || an % 400 == 0 )) ? true : false ); 
	}	
	,
	add:function(field,date,delta){
		var newDate=null;
		switch (field) {
		   case "year":		   		
    			newDate= new Date(parseInt(date.getFullYear())+1,date.getMonth(),date.getDate());
		 		break; 
	  	   case "month":
		  	    newDate= new Date(date.getFullYear(),date.getMonth()+delta,date.getDate());
		 		break; 		  
		   case "day":
			    newDate= new Date(date.getFullYear(),date.getMonth(),date.getDate()+delta);
		 		break; 		
		}
		return newDate;
	}
}

/*****************TO USE URL METHOD AND PROPERTIES**************************/
rialto.url={
	//Get the current location
	getUrl: function(){
		return window.location;
	},
	//Get the current search string
	getSearch: function(){
		return window.location.search;
	},
	//return an object with parameter name as propertie with his value
	//for example if in the url  you have name=smith&age=28
	//the return object will have properties .name=smith .age=28
	getObjectParameter:function(){
		var sPar=this.getSearch();
		var arrPar={};
		if(sPar.length > 1){
			sPar = sPar.substring(1, sPar.length);
			var arrTemp=sPar.split("&");		
			for(var i=0; i < arrTemp.length; i++) {
				var arrPair=arrTemp[i].split("=");
				arrPar[arrPair[0]]=arrPair[1];
			}
		}
		return arrPar;		
	},
	//return an array with each parameter is an array with his name and his value
	//for example if in the url  you have name=smith&age=28
	//the return array will be [['name','smith'],['age',28]]
	getArrayParameter:function(){
		var sPar=this.getSearch();
		var arrPar=new Array;
		if(sPar.length > 1){
			sPar = sPar.substring(1, sPar.length);
			var arrTemp=sPar.split("&");		
			for(var i=0; i < arrTemp.length; i++) {
				var arrPair=arrTemp[i].split("=");
				arrPar.push(arrPair);
			}
		}
		return arrPar;		
	}		
}
//fonction qui instancie un layout et retourne un pointeur sur la variable cr?e
function loadLayout(lName, vJS) {
//=================
	/*if ( (typeof __bootOK) == 'undefined') {
		__boot();
	}	
	*/
	var objLayout;
	
	if(lName){
		objLayout=eval("new "+lName+"();");   	    
	}
	if (vJS)
		vJS = objLayout;
	else
		return objLayout;
}
